package com.jac.alapores.manager;

import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jac.alapores.dao.AlaporesHeroRepository;
import com.jac.alapores.dao.AlaporesRepository;
import com.jac.alapores.dao.CategoryRepository;
import com.jac.alapores.dao.TerritoryRepository;
import com.jac.alapores.dao.UserRepository;
import com.jac.alapores.domain.Alapores;
import com.jac.alapores.domain.AlaporesCategory;
import com.jac.alapores.domain.AlaporesHero;
import com.jac.alapores.domain.AvailableRecruits;
import com.jac.alapores.domain.Territory;
import com.jac.alapores.domain.User;

import fi.foyt.foursquare.api.FoursquareApi;
import fi.foyt.foursquare.api.FoursquareApiException;
import fi.foyt.foursquare.api.Result;
import fi.foyt.foursquare.api.entities.Category;
import fi.foyt.foursquare.api.io.GAEIOHandler;

public class AlaporesManager
{
	private static final Logger logger = LoggerFactory.getLogger(AlaporesManager.class);
	
	private static final CategoryRepository CATEGORY_REPOSITORY = new CategoryRepository();
	private static final AlaporesRepository ALAPORES_REPOSITORY = new AlaporesRepository();
	private static final AlaporesHeroRepository ALAPORES_HERO_REPOSITORY = new AlaporesHeroRepository();
	private static final UserRepository USER_REPOSITORY = new UserRepository();
	private static final TerritoryRepository TERRITORY_REPOSITORY = new TerritoryRepository();
	
	private static final String CLIENT_ID = "TITX3OEHI0J1P54ZXCPYYES4H0OU4W5XVJBPZVWUGFOXVIJ4";
	private static final String CLIENT_SECRET = "XJG4BIXFGAV0QORVLGCLSQJXSTHC4O2A0WKMMOOMK4JKU52T";
	private static final String CALLBACK_URL = "http://alapores-service.appspot.com/oauth/callback";
	
	public void bootStrapCategories()
	{
		logger.info("Start bootStrapCategories");
		FoursquareApi foursquareApi = new FoursquareApi(CLIENT_ID, CLIENT_SECRET, CALLBACK_URL, new GAEIOHandler());
		
		try 
		{
			Result<Category[]> result = foursquareApi.venuesCategories();			
			insertCategories(result.getResult(), null);
		} 
		catch (FoursquareApiException e) 
		{
			logger.error("FoursquareApiException: " + e.getMessage());
		}
		
		logger.info("End bootStrapCategories");
	}
	
	public void addAlapores(Alapores alapores)
	{
		ALAPORES_REPOSITORY.create(alapores);
	}
	
	public void addAlaporesHero(AlaporesHero alaporesHero)
	{
		ALAPORES_HERO_REPOSITORY.create(alaporesHero);
	}
	
	public void addTerritory(Territory territory)
	{
		TERRITORY_REPOSITORY.create(territory);
	}
	
	public AvailableRecruits fetchAvailableRecruits(String venueID)
	{
		AvailableRecruits recruits = new AvailableRecruits();
		List<AlaporesHero> alaporesHeroList = ALAPORES_HERO_REPOSITORY.getAll();
		recruits.setAlapores(ALAPORES_REPOSITORY.getAll());
		
		Random random = new Random();
		int index = random.nextInt(ALAPORES_HERO_REPOSITORY.getAll().size());
		if (index < 0)
		{
			index = index * -1;
		}
		recruits.addAlaporesHero(alaporesHeroList.get(index));
		return recruits;
	}
	
	public boolean addRecruit(List<Long> alaporesIDs, String userID)
	{
		User user = USER_REPOSITORY.getById(userID);
		
		for (Long alaporesID : alaporesIDs)
		{
			Alapores alapores = ALAPORES_REPOSITORY.getById(alaporesID);
			if (alapores != null)
			{
				user.addAlapores(alapores);
			}
			else
			{
				AlaporesHero alaporesHero = ALAPORES_HERO_REPOSITORY.getById(alaporesID);
				if (alaporesHero != null)
				{
					user.addHeroAlapores(alaporesHero);
				}
			}
		}
		return true;
	}
	
	public void addUser(User user)
	{
		USER_REPOSITORY.create(user);
	}
	
	public User fetchCreateUser(String userID)
	{
		User user = USER_REPOSITORY.getById(userID);
		if (user == null)
		{
			user = new User(userID);
			user.setEnergyLevel(100);
		}
		return user;
	}
	
	private void insertCategories(Category[] categories, String parentCategoryID)
	{
		for (int i = 0; i < categories.length; i++)
		{
			Category category = categories[i];
			CATEGORY_REPOSITORY.create(new AlaporesCategory(category, parentCategoryID));
			if (category.getCategories() != null)
			{
				insertCategories(category.getCategories(), category.getId());
			}
		}
	}
}
